package com.iflytek.autofly.setting.widget;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iflytek.autofly.setting.R;

/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2016/1/2</td>
 * <td>14:55</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class LoadingAnimView extends LinearLayout {

    private static final java.lang.String TAG = "LoadingAnimView";
    private CycleView mBallView;
    static LoadingAnimView loadingAnimView;
    static WindowManager wm;
    static boolean isShow = false;

    public LoadingAnimView(Context context) {
        super(context);
        init(context);
    }

    public LoadingAnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public static void show(Context context, String text) {
        if (!isShow) {
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, // above the status bar!
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                            | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                            | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                            | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
                    PixelFormat.TRANSLUCENT);
//        final LoadingAnimView loadingAnimView = new LoadingAnimView(context);
            loadingAnimView = new LoadingAnimView(context);
            wm.addView(loadingAnimView, lp);
            loadingAnimView.addText(text);
            isShow = true;
        }
    }

    public static void hide() {
        if (isShow) {
            wm.removeView(loadingAnimView);
            isShow = false;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        CycleView mBallView = (CycleView) findViewById(R.id.cycle);
        mBallView.startAnim();
    }

//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        mBallView.postInvalidate();
//    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.loading_layout, this, true);
        mBallView = (CycleView) findViewById(R.id.cycle);
        //     setWillNotDraw(false);
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    public LoadingAnimView addText(String text) {
        TextView tv = (TextView) findViewById(R.id.loading_text_tv);
        tv.setText(text);
        return this;
    }
}
